package com.android.jumishangcheng.fragment.member;

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.jumishangcheng.R;
import com.android.jumishangcheng.fragment.group.Fragment_SAMain_TabList;
import com.android.jumishangcheng.fragment.group.Fragment_SeaAmoyTCList;
import com.android.jumishangcheng.model.member.SeaAmoyMain;
import com.android.jumishangcheng.model.member.SeaAmoyMainCallback;
import com.android.jumishangcheng.utils.ConstantsUrl;
import com.android.jumishangcheng.utils.HttpUtils;
import com.android.jumishangcheng.utils.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

import static com.android.jumishangcheng.R.id.main_top_bg;

/**
 * Created by Administrator on 2017/7/6.
 */

public class Fragment_SeaAmoy extends Fragment {
    @BindView(R.id.no_data)
    RelativeLayout noData;
    @BindView(R.id.no_data_tv)
    TextView noDataTv;
    @BindView(R.id.progress)
    RelativeLayout progress;
    @BindView(R.id.click_resetnetwork)
    LinearLayout clickResetnetwork;
    @BindView(R.id.tab)
    TabLayout tabLayout;
    @BindView(R.id.pager)
    ViewPager viewPager;
    @BindView(R.id.main_top_title)
    TextView mainTopTitle;
    @BindView(main_top_bg)
    Toolbar mainTopBg;

    private static final String ARG_PARAM1 = "param1";
    private LayoutInflater inflater;
    private View view;
    private FragmentActivity mcontext;
    private boolean networkConnected;
    private ArrayList<SeaAmoyMain.DataBean.Cat_list> cat_list = new ArrayList<>();
    private GroupFragmentPagerAdapter pagerAdapter;
    private String country_id = "";
    private int page = 1;

    public Fragment_SeaAmoy() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        view = inflater.inflate(R.layout.fragment_seaamoymain, container, false);
        mcontext = getActivity();
        ButterKnife.bind(this, view);
        mainTopTitle.setText("全球购");
        mainTopTitle.setTextColor(Color.parseColor("#ffffff"));
        mainTopBg.setBackgroundColor(Color.parseColor("#FF2741"));
        GetSAMain();
        return view;
    }

    private void initUI() {
        pagerAdapter = new GroupFragmentPagerAdapter(getChildFragmentManager());
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);
    }


    private class GroupFragmentPagerAdapter extends FragmentPagerAdapter {
        FragmentManager fm;
        public GroupFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
            this.fm = fm;
        }

        @Override
        public Fragment getItem(int position) {
            if (position == 0) {
                return Fragment_SAMain_TabList.newInstance();
            } else {
                return Fragment_SeaAmoyTCList.newInstance(cat_list.get(position).getId());
            }
        }

        @Override
        public int getCount() {
            return cat_list.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return cat_list.get(position).getCatname();
        }
    }

    @OnClick(R.id.click_resetnetwork_refresh)
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.click_resetnetwork_refresh:
                ObjectUtils.GetDataNet(clickResetnetwork, progress, 3);
                GetSAMain();
                break;
        }
    }

    private void GetSAMain() {
        networkConnected = ObjectUtils.isNetworkConnected(mcontext);
        if (networkConnected) {
            String address = ConstantsUrl.domain + ConstantsUrl.NATIONLISTDATA;
            HttpUtils.get(address, null, new SeaAmoyMainCallback() {

                @Override
                public void onError(Call call, Exception e, int id) {
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 2);
                }

                @Override
                public void onResponse(SeaAmoyMain response, int id) {
                    if (response == null) {
                        ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
                        return;
                    }
                    ObjectUtils.GetDataNet(clickResetnetwork, progress, 1);
                    if (response.getCode() == 0) {
                        cat_list.clear();
                        cat_list.add(new SeaAmoyMain.DataBean.Cat_list("0", "全部"));
                        SeaAmoyMain.DataBean data = response.getData();
                        if (data != null && ObjectUtils.listHasData(data.getCat_list())) {
                            List<SeaAmoyMain.DataBean.Cat_list> catLists = data.getCat_list();
                            cat_list.addAll(catLists);
                        }
                        initUI();
                    } else {
                        noData.setVisibility(View.VISIBLE);
                        noDataTv.setVisibility(View.VISIBLE);
                        noDataTv.setText(response.getMsg());
                    }
                }
            });
        } else {
            ObjectUtils.GetDataNet(clickResetnetwork, progress, 4);
            ObjectUtils.toast(mcontext, "连接连接出现异常");
        }
    }
}
